package com.keba.lamp.complaint.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.keba.basic.context.ContextUtil;
import com.keba.basic.database.mybatis.conditions.Wraps;
import com.keba.basic.utils.ArgumentAssert;
import com.keba.basic.utils.BeanPlusUtil;
import com.keba.lamp.common.constant.AppendixType;
import com.keba.lamp.complaint.entity.AccountDetail;
import com.keba.lamp.complaint.enumeration.TransactionTypeEnum;
import com.keba.lamp.complaint.enumeration.TransferTypeEnum;
import com.keba.lamp.complaint.service.AccountDetailService;
import com.keba.lamp.complaint.vo.param.AccountPrepaymentParamVO;
import com.keba.lamp.complaint.vo.save.AccountDetailSaveVO;
import com.keba.lamp.file.service.AppendixService;
import com.keba.lamp.model.vo.save.AppendixSaveVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.keba.lamp.complaint.service.AccountPrepaymentService;
import com.keba.basic.base.service.impl.SuperServiceImpl;
import com.keba.lamp.complaint.manager.AccountPrepaymentManager;
import com.keba.lamp.complaint.entity.AccountPrepayment;
import com.keba.lamp.complaint.vo.save.AccountPrepaymentSaveVO;
import com.keba.lamp.complaint.vo.update.AccountPrepaymentUpdateVO;
import com.keba.lamp.complaint.vo.result.AccountPrepaymentResultVO;
import com.keba.lamp.complaint.vo.query.AccountPrepaymentPageQuery;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 * 业务实现类
 * 预付款账户
 * </p>
 *
 * @author twg
 * @date 2025-05-15 20:32:18
 * @create [2025-05-15 20:32:18] [twg] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class AccountPrepaymentServiceImpl extends SuperServiceImpl<AccountPrepaymentManager, Long, AccountPrepayment> implements AccountPrepaymentService {

    private final AccountDetailService accountDetailService;

    private final AppendixService appendixService;

    @Override
    public AccountPrepayment getByTenantId(Long tenantId) {
        return getSuperManager().getOne(Wraps.<AccountPrepayment>lbQ().eq(AccountPrepayment::getTenantId, tenantId));
    }

    @Override
    public <SaveVO> AccountPrepayment saveBefore(SaveVO saveVO) {
        AccountPrepaymentSaveVO saveVo = (AccountPrepaymentSaveVO) saveVO;
        return BeanPlusUtil.toBean(saveVo, AccountPrepayment.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deduction(AccountPrepaymentParamVO prepaymentParamVO) {
        AccountPrepayment accountPrepayment = this.getById(prepaymentParamVO.getId());
        BigDecimal balance = accountPrepayment.getBalance();
        BigDecimal amount = prepaymentParamVO.getAmount();

//        ArgumentAssert.isFalse(!TransferTypeEnum.RECHARGE.getCode().equals(prepaymentParamVO.getTransferTypeEnum().getCode())
//                && balance.compareTo(amount) < 0, "账户余额不足");
        if (TransferTypeEnum.RECHARGE.getCode().equals(prepaymentParamVO.getTransferTypeEnum().getCode())) {
            accountPrepayment.setBalance(balance.add(amount));
        } else {
            accountPrepayment.setBalance(balance.subtract(amount));
        }

        superManager.update(accountPrepayment, Wraps.<AccountPrepayment>lbQ().eq(AccountPrepayment::getId, accountPrepayment.getId())
                .eq(AccountPrepayment::getTenantId, prepaymentParamVO.getTenantId())
                .eq(AccountPrepayment::getBalance, balance));

        AccountDetail accountDetail = accountDetailService.save(new AccountDetailSaveVO().setCode(DateUtil.format(DateUtil.date(), DatePattern.PURE_DATE_PATTERN) + DateUtil.currentSeconds()).setAccountCode(accountPrepayment.getCode())
                .setAccountType(accountPrepayment.getType()).setBalance(accountPrepayment.getBalance())
                .setRemark(prepaymentParamVO.getRemark())
                .setBizType(prepaymentParamVO.getBizType())
                .setCaseId(prepaymentParamVO.getCaseId())
                .setBizDesc(Objects.isNull(prepaymentParamVO.getCaseId()) ? "账户" + prepaymentParamVO.getTransferTypeEnum().getDesc() : prepaymentParamVO.getCaseName())
                .setBizTime(LocalDateTime.now()).setBookingTime(LocalDateTime.now())
                .setAmount(amount).setTenantId(ContextUtil.getTenantId()).setType(prepaymentParamVO.getTransferTypeEnum().getCode())
                .setIncomeExpenseType(TransferTypeEnum.RECHARGE.getCode().equals(prepaymentParamVO.getTransferTypeEnum().getCode()) ? TransactionTypeEnum.INCOME.getCode() : TransactionTypeEnum.EXPENSE.getCode()));


        if (Objects.nonNull(prepaymentParamVO.getAppendixIcon())) {
            boolean flag = appendixService.save(AppendixSaveVO.build(accountDetail.getId(), AppendixType.Complaint.ACCOUNT__EVIDENCE__IMAGE, prepaymentParamVO.getAppendixIcon()));
        }

    }
}


